<template>
    <el-container>
      <!-- 头部区域 -->
    <el-header>
      <div class="logo-container">
        <img src="../assets/img/heima.png">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 菜单栏区域 -->
      <el-aside :width="this.isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="controlCollapse">|||</div>
         <el-menu
         :collapse="isCollapse"
         :collapse-transition="false"
            router
            :unique-opened="true"
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF">
            <!-- 一级菜单 -->
            <!-- 这里的index只接受字符串，不接受数值 -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click="saveActiveState('/'+subItem.path)">
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

export default {
  data () {
    return {
      // 存储菜单选项
      menuList: [],
      // 存放一级菜单对应的图标
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 用来控制菜单栏是否折叠
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    // 实现退出功能
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenuData () {
      const { data: res } = await this.$http.get('menus')
      this.menuList = res.data
      // console.log(res)
    },
    // 控制菜单栏是否折叠
    controlCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 用来保存菜单栏的激活状态的路径
    saveActiveState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  created () {
    this.getMenuData()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style scoped>
/* 这是让整个布局撑满全屏 */
.el-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  color: #ffffff;
  font-size: 20px;
  /* padding-left: 0px; */
  display: flex;
  /* 这个是为了让左右均匀 */
  justify-content: space-between;
  /* 这个是为了让按钮在垂直方向上居中 */
  align-items: center;
}
/* 这个是为了让左边图片和文字区域的文字在垂直方向上居中 */
.logo-container {
  display: flex;
  align-items: center;
}
.logo-container span{
  margin-left: 15px;
}
.el-aside{
  background: #333744;
}
.el-main{
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background: #4A5064;
  text-align: center;
  color: #ffffff;
  letter-spacing: 0.2rem;
  cursor: pointer;
  font-size: 10px;
  line-height: 24px;
}
</style>
